package com.toolboxkit.stream;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 流处理工具类
 * 提供流的转换、处理等常用操作
 */
public class StreamUtils {
    
    private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
    
    /**
     * 将InputStream转换为字节数组
     *
     * @param inputStream 输入流
     * @return 字节数组
     * @throws IOException IO异常
     */
    public static byte[] toByteArray(InputStream inputStream) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int nRead;
        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        return buffer.toByteArray();
    }
    
    /**
     * 将InputStream转换为字符串
     *
     * @param inputStream 输入流
     * @return 字符串
     * @throws IOException IO异常
     */
    public static String toString(InputStream inputStream) throws IOException {
        return toString(inputStream, DEFAULT_CHARSET);
    }
    
    /**
     * 将InputStream转换为字符串
     *
     * @param inputStream 输入流
     * @param charset 字符集
     * @return 字符串
     * @throws IOException IO异常
     */
    public static String toString(InputStream inputStream, Charset charset) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line).append(System.lineSeparator());
            }
        }
        return stringBuilder.toString();
    }
    
    /**
     * 将字符串转换为InputStream
     *
     * @param content 字符串内容
     * @return InputStream
     */
    public static InputStream toInputStream(String content) {
        return toInputStream(content, DEFAULT_CHARSET);
    }
    
    /**
     * 将字符串转换为InputStream
     *
     * @param content 字符串内容
     * @param charset 字符集
     * @return InputStream
     */
    public static InputStream toInputStream(String content, Charset charset) {
        return new ByteArrayInputStream(content.getBytes(charset));
    }
    
    /**
     * 复制流数据
     *
     * @param input 输入流
     * @param output 输出流
     * @throws IOException IO异常
     */
    public static void copy(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = input.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
    }
    
    /**
     * 将InputStream中的数据按行读取为列表
     *
     * @param inputStream 输入流
     * @return 行列表
     * @throws IOException IO异常
     */
    public static List<String> readLines(InputStream inputStream) throws IOException {
        return readLines(inputStream, DEFAULT_CHARSET);
    }
    
    /**
     * 将InputStream中的数据按行读取为列表
     *
     * @param inputStream 输入流
     * @param charset 字符集
     * @return 行列表
     * @throws IOException IO异常
     */
    public static List<String> readLines(InputStream inputStream, Charset charset) throws IOException {
        List<String> lines = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        }
        return lines;
    }
    
    /**
     * 安全关闭流
     *
     * @param closeable 可关闭的对象
     */
    public static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException ignored) {
                // 忽略关闭异常
            }
        }
    }
}
